//
//  FQHomeViewController.swift
//  SwiftProject
//
//  Created by 董駸 on 2023/4/12.
//

import UIKit

class FQHomeViewController: BaseViewController, UITableViewDataSource, UITableViewDelegate {
    let tableView = UITableView(frame: .zero, style: .plain)
    let headerView = FQHomeHeaderView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 500))
    var dataArray : [FQOrganizationModel] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationBarType = .light
    }
    
    override func initSubviews() {
        super.initSubviews()
        
        headerView.buttonBlock = { [unowned self] in
            if self.dataArray.isEmpty {
                return
            }
            NetworkManager.manager.request(targetType: .routeProductInfo, responseModelType: NetworkBaseModel<FQOrganizationModel>.self) { responseModel in
                if responseModel.code != .successCode {
                    QMUITips.showError(responseModel.msg)
                    return
                }
                let vc = BaseWebViewController(.network(responseModel.data?.url))
                self.navigationController?.qmui_pushViewController(vc, animated: true)
            }
        }
        
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(FQHomeProductTableViewCell.self, forCellReuseIdentifier: "cell")
        tableView.rowHeight = 110~
        tableView.separatorStyle = .none
        tableView.backgroundColor = view.backgroundColor
        tableView.tableHeaderView = headerView
        view.addSubview(tableView)
        
        tableView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(NavigationContentTop)
        }
        
        requestData()
    }
    
    override func requestData() {
        NetworkManager.manager.request(targetType: .productInfo, responseModelType: NetworkBaseListModel<FQOrganizationModel>.self) { responseModel in
            if responseModel.code != .successCode {
                QMUITips.showError(responseModel.msg)
                return
            }
            self.dataArray = responseModel.data!
            self.tableView.reloadData()
        }
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        dataArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! FQHomeProductTableViewCell
        cell.model = dataArray[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        NetworkManager.manager.request(targetType: .productInfoUrl(productId: dataArray[indexPath.row].id), responseModelType: NetworkBaseModel<FQOrganizationModel>.self) { responseModel in
            if responseModel.code != .successCode {
                QMUITips.showError(responseModel.msg)
                return
            }
            let vc = BaseWebViewController(.network(responseModel.data?.url))
            self.navigationController?.qmui_pushViewController(vc, animated: true)
        }
    }
    
    @objc func buttonClick() {
        
    }

    override func setupNavigationItems() {
        super.setupNavigationItems()
        navigationItem.leftBarButtonItem = .qmui_item(with: QMUINavigationButton(image: UIImage(named: "icon_baoyinWallet2")), target: self, action: nil)
        navigationItem.leftBarButtonItem?.customView?.isUserInteractionEnabled = false
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "icon_home_kefu")?.withRenderingMode(.alwaysTemplate), style: .done, target: self, action: #selector(barClick))
        navigationItem.rightBarButtonItem?.tintColor = .black
    }
    
    @objc func barClick() {
        
    }
    
    override func customNavigationBarTransitionKey() -> String? {
        "home"
    }
}
